<?php

namespace App\Http\Requests\Project;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class PmChildProjectRequest extends FormRequest
{
    /**
     * 表示验证器是否应在第一个规则失败时停止
     *
     * @var bool
     */
    protected $stopOnFirstFailure = true;

    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
     */
    public function rules(): array
    {
        $rules = [
            'direct_donate_budget' => 'required',
            'activity_cost_budget' => 'required',
            'periods' => 'required',
            'submit_type' => 'required',
        ];

        if ($this->route('id')) {
            /* update data */
            $rules['project_name'] = ['required', Rule::unique('App\Models\PmProjectBasic')->ignore($this->route('id'))];
        } else {
            /* store data */
            $rules['project_name'] = ['required', Rule::unique('App\Models\PmProjectBasic')->where('deleted_at')];
            $rules['parent_id'] = ['required', Rule::exists('App\Models\PmProjectBasic', 'id')];
        }

        return $rules;
    }

    /**
     * 获取已定义验证规则的错误消息
     *
     * @return string[]
     */
    public function messages()
    {
        return [
            'project_name.required' => '子项目名称不能为空',
            'project_name.unique' => '项目名称已存在',
            'direct_donate_budget.required' => '直接捐赠预算不能为空',
            'activity_cost_budget.required' => '活动成本预算不能为空',
            'periods.required' => '计划拨付期数不能为空',
            'submit_type.required' => '保存类型不能为空',
            'parent_id.required' => '所属父项目不能为空',
            'parent_id.exists' => '父项目不存在',
        ];
    }
}
